﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using MSOfficeTools;
using System.Threading;
using Microsoft.Win32;

namespace InsertTestsToDocumentation
{

    delegate void myMethodDelegate();

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {
        string testDirString;
        string documentPath;
        string outputFile;
        Thread fillDocument;

        myMethodDelegate del;

        public MainWindow()
        {
            InitializeComponent();
            del = new myMethodDelegate(PrintReady);
        }

        private void btnTestsDir_Click(object sender, RoutedEventArgs e)
        {
            string initialDir = (string)Microsoft.Win32.Registry.CurrentUser.GetValue("TestDirectory");
            System.Windows.Forms.OpenFileDialog testDir = new System.Windows.Forms.OpenFileDialog();
            testDir.InitialDirectory = initialDir;
            if (testDir.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                testDirString = testDir.FileName;
                testDirString = System.IO.Path.GetDirectoryName(testDirString);
                txtTestsDir.Text = testDirString;
                Microsoft.Win32.Registry.CurrentUser.SetValue("TestDirectory", testDirString);
            }
        }

        private void btnSelectDocument_Click(object sender, RoutedEventArgs e)
        {
            string initialDir = (string)Microsoft.Win32.Registry.CurrentUser.GetValue("TemplateDirectory");
            System.Windows.Forms.OpenFileDialog testPath = new System.Windows.Forms.OpenFileDialog();
            testPath.InitialDirectory = initialDir;
            if (testPath.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                documentPath = testPath.FileName;
                txtDocumentationSourceFile.Text = documentPath;
                initialDir = System.IO.Path.GetDirectoryName(documentPath);
                Microsoft.Win32.Registry.CurrentUser.SetValue("TemplateDirectory", initialDir);
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            txtStatus.Text = "Working";
            fillDocument = new Thread(new ThreadStart(FillTestsInDocumentation));
            fillDocument.Start();
        }

        private void PrintReady()
        {
            txtStatus.Text = "Ready";
        }

        public void FillTestsInDocumentation()
        {
            List<string> stringsToReplace = new List<string>();
            for (int i = 0; i < 20; i++)
            {
                stringsToReplace.Add(i.ToString());
            }

            List<string> stringsToReplaceWith = new List<string>();
            for (int i = 1; i <= 10; i++)
            {
                StringBuilder toReplaceWhith = new StringBuilder();

                using (StreamReader sr = new StreamReader(string.Format("{0}\\test_{1:00}.in.txt", testDirString, i)))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        line.Trim();
                        if (!string.IsNullOrEmpty(line))
                        {
                            toReplaceWhith.AppendLine(line);
                        }
                    }
                }

                stringsToReplaceWith.Add(toReplaceWhith.ToString().TrimEnd(new char[] {'\r', '\n'}));

                toReplaceWhith.Clear();
                using (StreamReader sr = new StreamReader(string.Format("{0}\\test_{1:00}.out.txt", testDirString, i)))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        line.Trim();
                        if (!string.IsNullOrEmpty(line))
                        {
                            toReplaceWhith.AppendLine(line);
                        }
                    }
                }

                stringsToReplaceWith.Add(toReplaceWhith.ToString().TrimEnd(new char[] { '\r', '\n' }));
            }

            MicrosoftWordTools.ReplaceStringsInWordDocument(stringsToReplace, stringsToReplaceWith, documentPath, outputFile);
            Dispatcher.Invoke(del);
        }

        private void btnOutput_Click(object sender, RoutedEventArgs e)
        {
            string initialDir = (string)Microsoft.Win32.Registry.CurrentUser.GetValue("NewDocumentationDirectory");
            System.Windows.Forms.OpenFileDialog outputFile = new System.Windows.Forms.OpenFileDialog();
            outputFile.InitialDirectory = initialDir;
            outputFile.CheckFileExists = false;
            if (outputFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.outputFile = outputFile.FileName;
                txtOutputDocumentationDir.Text = this.outputFile;
                initialDir = System.IO.Path.GetDirectoryName(this.outputFile);
                Microsoft.Win32.Registry.CurrentUser.SetValue("NewDocumentationDirectory", initialDir);
            }
        }
    }
}
